// 数据处理工具函数
import { OptionsItem } from '@com/Select/index';

//将对象数组某一键值拼接成字符串
export const objArrValueJoin = <K extends keyof T, T>(key: K, list: T[]): string => {
  return list
    .map((obj) => {
      return String(obj[key]);
    })
    .join('，');
};

//过滤optionList中含有对应value的项，并且将value转成label展示
export const getSelectLabel = (
  value: string | number | string[] | number[],
  optionList: OptionsItem[],
): string => {
  let arr = [];
  if (value) {
    for (let i = 0; i < optionList.length; i++) {
      if (String(value).includes(String(optionList[i].value))) {
        arr.push(optionList[i]);
      }
    }
    if (arr.length === 1) {
      return arr[0].label;
    } else if (arr.length > 1) {
      return objArrValueJoin('label', arr);
    } else {
      return '';
    }
  } else {
    return '';
  }
};

// 过滤函数，如果是函数类型先执行，否则直接返回
export const generateFunction = (
  param: string | ((rowData: Record<string, unknown>, index?: number) => any),
  rowData?: Record<string, unknown>,
  index?: number,
) => {
  if (typeof param === 'function') {
    return param(rowData, index);
  } else {
    return param;
  }
};
